/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cda.pager;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

/**
 *
 * @author Shyzkanza
 */
public class CreerCarteFidelite extends Activity {
    
    private EditText                            nomMagasin;
    private EditText                            nomPersonne;
    
    private TextView                            typeBarres;
    private TextView                            codeBarres;
    
    private Button                              scanCode;
    private Button                              annule;
    private Button                              valide;

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        // ToDo add your GUI initialization code here     
        setContentView(R.layout.createcartefidel);
        
        initInterface();
    }
    
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent intent) {
        if (requestCode == 0) {
            if (resultCode == RESULT_OK) {
                codeBarres.setText(intent.getStringExtra("SCAN_RESULT"));
                typeBarres.setText(intent.getStringExtra("SCAN_RESULT_FORMAT"));
            } else if (resultCode == RESULT_CANCELED) {
                Toast.makeText(this, "Echec", Toast.LENGTH_LONG).show();
            }
        }
    }
    
    private void initInterface(){
        valide = (Button) findViewById(R.id.validecreationcarte);
        annule = (Button) findViewById(R.id.annulecreationcarte);
        scanCode = (Button) findViewById(R.id.scancodefidelite);
        
        typeBarres = (TextView) findViewById(R.id.typefidelite);
        codeBarres = (TextView) findViewById(R.id.codefidelite);
        
        nomMagasin = (EditText) findViewById(R.id.nommagasinfidelite);
        nomPersonne = (EditText) findViewById(R.id.nompersonnefidelite);
        
        valide.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                String mag = "";
                String nom = "";
                String type = typeBarres.getText().toString();
                String code = codeBarres.getText().toString();
                
                if(nomMagasin.getText().toString().equals("")){
                    mag = "Default";
                }else{
                    mag = nomMagasin.getText().toString();
                }
                
                if(nomPersonne.getText().toString().equals("")){
                    nom = "Default";
                }else{
                    nom = nomPersonne.getText().toString();
                }
                     
                MyPagerAdapter.CBDDF.addCarteFidelite(mag, type, code, nom);
                MyPagerAdapter.MAJListeFideliteStatic();
                finish();
            }
        });
        
        annule.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                finish();
            }
        });
        
        scanCode.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                Intent intent = new Intent("com.google.zxing.client.android.SCAN");
                startActivityForResult(intent, 0);
            }
        });
    }
}
